import bean.ListNode;

public class LeetCode206 {
    public static void main(String[] args) {
        ListNode.printNodeList(reverseList(ListNode.createListNodes(new int[]{1,2 ,3, 4, 5})));
    }

    /**
     * 思路：
     * @param head
     * @return
     */
    public static ListNode reverseList(ListNode head) {
        if (head == null){
            return head;
        }

        if (head.next == null){
            return head;
        }

        ListNode lastNode = head;
        ListNode headNode = head.next;
        lastNode.next = head.next.next;
        headNode.next = lastNode;
        while (lastNode.next != null){
            ListNode tempNode = lastNode.next;
            lastNode.next = tempNode.next;
            tempNode.next = headNode;
            headNode = tempNode;
        }

        return headNode;
    }
}
